from django.db import models
from Administrative_office_system.utils.BaseModel import BaseModel


class UnionSeal(BaseModel):
    seal_name = models.CharField(max_length=255, verbose_name='印章名称', default='')
    status = models.CharField(max_length=10, verbose_name='是否可用', default='')


class OfficialSeal(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '科室负责人'),
        ('two', '工会办公室'),
        ('three', '审计科'),
        ('four', '工会主席/工会分管领导'),
        ('five', '工会办公室'),
        ('six', '现场盖章'),
        ('refuse', '拒绝'),
        ('end', '结束'),
        ('stop', '终止'),
    )
    FILETYPE = (
        ("1", "协议类"),
        ("2", "非协议类")
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL)
    company = models.ForeignKey('administrative.Company', verbose_name="申请科室", blank=False, null=False, on_delete=models.CASCADE)
    reason = models.CharField(max_length=255, verbose_name='用印事由', default='')
    printing = models.CharField(max_length=50, verbose_name='用印次数', default='')
    file_type = models.CharField(max_length=10, verbose_name='文件类型', choices=FILETYPE, default='1')
    use_seal = models.ManyToManyField('UnionSeal', verbose_name='使用印章', related_name='UseSeal', blank=True)
    note = models.CharField(max_length=255, verbose_name='备注', default='')
    can_revise = models.CharField(max_length=10, verbose_name='是否可以修改', default='1')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='official_seal_audit_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='official_seal_already_audit_users', blank=True)

    def get_state_str(self):
        if self.state == 'refuse':
            return '拒绝'
        elif self.state == 'end':
            return '结束'
        elif self.state == 'stop':
            return '终止'
        elif self.state == 'draft':
            return '草稿'
        else:
            return '审核中'

    def get_file_type(self):
        if self.file_type == '1':
            return '协议类'
        else:
            return '非协议类'


class OfficialSealRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '科室负责人'),
        ('two', '工会办公室'),
        ('three', '审计科'),
        ('four', '工会主席/工会分管领导'),
        ('five', '工会办公室'),
        ('six', '现场盖章')
    )
    parent_id = models.ForeignKey('OfficialSeal', verbose_name="工会用印流程", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10, choices=NODE, default='one')

    class Meta:
        managed = True
        verbose_name = '工会用印流程'
        verbose_name_plural = verbose_name


class EconomicCountersign(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '科室负责人'),
        ('two', '财务科'),
        ('three', '监察室'),
        ('four', '审计科'),
        ('five', '工会主席/分管领导'),
        ('six', '工会办公室'),
        ('seven', '现场盖章'),
        ('refuse', '拒绝'),
        ('end', '结束'),
        ('stop', '终止'),
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL)
    company = models.ForeignKey('administrative.Company', verbose_name="申请科室", related_name="apply_department", blank=True, null=False, on_delete=models.CASCADE, default="")
    project_company = models.ForeignKey('administrative.Company', verbose_name="立项科室", related_name="project_approval_department", blank=True, null=True, on_delete=models.CASCADE, default="")
    topics_id = models.ForeignKey('administrative.Topics', verbose_name='关联议题流程', related_name='EconomicCountersignAssociation', blank=True, null=True, on_delete=models.SET_NULL)
    contract_name = models.CharField(max_length=255, verbose_name='合同名称', default='')
    contract_number = models.CharField(max_length=50, verbose_name='合同编号', default='')
    other_unit = models.CharField(max_length=100, verbose_name='对方单位', default='1')
    contract_money = models.CharField(max_length=100, verbose_name='合同金额', default='')
    money_capital = models.CharField(max_length=100, verbose_name='金额大写', default='')
    main_terms = models.TextField(verbose_name='合同内容主要条款', default='')
    note = models.CharField(max_length=255, verbose_name='备注', default='')
    can_revise = models.CharField(max_length=10, verbose_name='是否可以修改', default='1')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='economic_countersign_audit_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='economic_countersign_already_audit_users', blank=True)

    def get_state_str(self):
        if self.state == 'refuse':
            return '拒绝'
        elif self.state == 'end':
            return '结束'
        elif self.state == 'stop':
            return '终止'
        elif self.state == 'draft':
            return '草稿'
        else:
            return '审核中'


class EconomicCountersignRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '科室负责人'),
        ('two', '财务科'),
        ('three', '监察室'),
        ('four', '审计科'),
        ('five', '工会主席/分管领导'),
        ('six', '工会办公室'),
        ('seven', '现场盖章')
    )
    parent_id = models.ForeignKey('EconomicCountersign', verbose_name="工会经济合同会签", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10, choices=NODE, default='one')

    class Meta:
        managed = True
        verbose_name = '工会经济合同会签'
        verbose_name_plural = verbose_name


class PaymentProcess(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '科室负责人'),
        ('two', '工会办公室'),
        ('three', '工会会计'),
        ('four', '财务科'),
        ('five', '工会主席/分管领导'),
        ('six', '财务科支付'),
        ('refuse', '拒绝'),
        ('end', '结束'),
        ('stop', '终止'),
    )
    PayType = (
        ('1', '合同付款'),
        ('2', '固定资产/维修维保付款'),
        ('3', '材料付款'),
        ('4', '餐补付款'),
        ('5', '学科经费'),
        ('6', '科研经费'),
        ('7', '一般付款'),
        ('8', '工会付款'),
    )
    FundingSource = (
        ('1', '自有经费'),
        ('2', '学科经费'),
        ('3', '科研经费'),
        ('4', '财政拨款'),
        ('5', '贴息贷款'),
        ('6', '专项债券'),
        ('7', '工会经费'),
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL)
    company = models.ForeignKey('administrative.Company', verbose_name="申请科室", blank=True, null=False, on_delete=models.CASCADE, default="")
    topics_id = models.ForeignKey('administrative.Topics', verbose_name='关联议题流程', related_name='PaymentAssociation', blank=True, null=True, on_delete=models.SET_NULL)
    reason = models.CharField(max_length=255, verbose_name='付款事由', default='')
    source = models.CharField('经费来源', max_length=10, choices=FundingSource, default='7')
    payee = models.CharField(max_length=255, verbose_name='收款单位', default='1')
    account_number = models.CharField(max_length=255, verbose_name='收款账号', default='1')
    total_amount = models.CharField(max_length=100, verbose_name='总金额', default='')
    money_capital = models.CharField(max_length=100, verbose_name='金额大写', default='')
    pay_type = models.CharField('付款类型', max_length=10, choices=PayType, default='8')
    bank = models.TextField(verbose_name='开户行', default='')
    note = models.CharField(max_length=255, verbose_name='备注', default='')
    can_revise = models.CharField(max_length=10, verbose_name='是否可以修改', default='1')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='payment_process_audit_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='payment_process_already_audit_users', blank=True)

    def get_state_str(self):
        if self.state == 'refuse':
            return '拒绝'
        elif self.state == 'end':
            return '结束'
        elif self.state == 'stop':
            return '终止'
        elif self.state == 'draft':
            return '草稿'
        else:
            return '审核中'

    def get_source_str(self):
        if self.source == "1":
            return '自有经费'
        elif self.source == "2":
            return '学科经费'
        elif self.source == "3":
            return '科研经费'
        elif self.source == "4":
            return '财政拨款'
        elif self.source == "5":
            return '贴息贷款'
        elif self.source == "6":
            return '专项债券'
        elif self.source == "7":
            return '工会经费'
        else:
            return ""

    def get_pay_type(self):
        if self.pay_type == "1":
            return '合同付款'
        elif self.pay_type == "2":
            return '固定资产/维修维保付款'
        elif self.pay_type == "3":
            return '材料付款'
        elif self.pay_type == "4":
            return '餐补付款'
        elif self.pay_type == "5":
            return '学科经费'
        elif self.pay_type == "6":
            return '科研经费'
        elif self.pay_type == "8":
            return '工会经费'
        else:
            return '一般付款'


class PaymentProcessRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '科室负责人'),
        ('two', '工会办公室'),
        ('three', '工会会计'),
        ('four', '财务科'),
        ('five', '工会主席/分管领导'),
        ('six', '财务科支付')
    )
    parent_id = models.ForeignKey('PaymentProcess', verbose_name="工会付款流程", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10, choices=NODE, default='one')

    class Meta:
        managed = True
        verbose_name = '工会付款流程'
        verbose_name_plural = verbose_name


class EventApproval(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '科室负责人'),
        ('two', '分管领导'),
        ('three', '工会办公室'),
        ('four', '工会主席/分管领导'),
        ('five', '工会归档'),
        ('refuse', '拒绝'),
        ('end', '结束'),
        ('stop', '终止'),
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL)
    company = models.ForeignKey('administrative.Company', verbose_name="申请科室", blank=True, null=False, on_delete=models.CASCADE, default="")
    reason = models.CharField(max_length=255, verbose_name='活动事由', default='')
    start_time = models.CharField('开始时间', max_length=100, default='')
    end_time = models.CharField(max_length=100, verbose_name='结束时间', default='')
    venue = models.CharField(max_length=255, verbose_name='活动地点', default='')
    participants = models.CharField(max_length=100, verbose_name='参与人数', default='')
    note = models.CharField(max_length=255, verbose_name='备注', default='')
    additional = models.CharField(max_length=10, verbose_name='是否补录', default='0')
    can_revise = models.CharField(max_length=10, verbose_name='是否可以修改', default='1')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='event_approval_audit_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='event_approval_already_audit_users', blank=True)

    def get_state_str(self):
        if self.state == 'refuse':
            return '拒绝'
        elif self.state == 'end':
            return '结束'
        elif self.state == 'stop':
            return '终止'
        elif self.state == 'draft':
            return '草稿'
        else:
            return '审核中'


class EventApprovalRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '科室负责人'),
        ('two', '分管领导'),
        ('three', '工会办公室'),
        ('four', '工会主席/分管领导'),
        ('five', '工会归档')
    )
    parent_id = models.ForeignKey('EventApproval', verbose_name="工会团体活动事前审批流程", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10, choices=NODE, default='one')

    class Meta:
        managed = True
        verbose_name = '工会团体活动事前审批流程'
        verbose_name_plural = verbose_name


class MaterialsApproval(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '科室负责人'),
        ('two', '分管领导'),
        ('three', '工会办公室'),
        ('four', '工会主席/分管领导'),
        ('five', '工会归档'),
        ('refuse', '拒绝'),
        ('end', '结束'),
        ('stop', '终止'),
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL)
    company = models.ForeignKey('administrative.Company', verbose_name="申请科室", blank=True, null=False, on_delete=models.CASCADE, default="")
    reason = models.CharField(max_length=255, verbose_name='申请事由', default='')
    goods_type = models.CharField(max_length=100, verbose_name='物资类型', default='')
    goods_number = models.CharField(max_length=100, verbose_name='物资数量', default='')
    total_amount = models.CharField(max_length=255, verbose_name='预算总金额', default='')
    money_capital = models.CharField(max_length=255, verbose_name='金额大写', default='')
    note = models.CharField(max_length=255, verbose_name='备注', default='')
    can_revise = models.CharField(max_length=10, verbose_name='是否可以修改', default='1')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='materials_approval_audit_users', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='materials_approval_already_audit_users', blank=True)

    def get_state_str(self):
        if self.state == 'refuse':
            return '拒绝'
        elif self.state == 'end':
            return '结束'
        elif self.state == 'stop':
            return '终止'
        elif self.state == 'draft':
            return '草稿'
        else:
            return '审核中'


class MaterialsApprovalRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '科室负责人'),
        ('two', '分管领导'),
        ('three', '工会办公室'),
        ('four', '工会主席/分管领导'),
        ('five', '工会归档')
    )
    parent_id = models.ForeignKey('MaterialsApproval', verbose_name="工会物资申购流程", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10, choices=NODE, default='one')

    class Meta:
        managed = True
        verbose_name = '工会物资申购流程'
        verbose_name_plural = verbose_name


# 门诊换班
class OutpatientScheduling(BaseModel):
    STATE = (
        ('draft', '草稿'),
        ('one', '科室负责人'),
        ('two', '门诊部办公室'),
        ('refuse', '拒绝'),
        ('end', '结束'),
        ('stop', '终止'),
    )
    TYPE = (
        ('1', "紧急停诊"),
        ('2', "计划停诊"),
        ('3', "长期停诊"),
        ('4', "换诊"),
    )
    state = models.CharField('状态', max_length=10, choices=STATE, default='one')
    apply_user = models.ForeignKey('personal_center.User', verbose_name="申请人", blank=True, null=True, on_delete=models.SET_NULL)
    company = models.ForeignKey('administrative.Company', verbose_name="申请科室", blank=True, null=False, on_delete=models.CASCADE, default="")
    current_audit_users = models.ManyToManyField('personal_center.User', verbose_name='当前审核人员', related_name='SchedulingAuditUsers', blank=True)
    audit_users_already = models.ManyToManyField('personal_center.User', verbose_name='已审核人员', related_name='SchedulingAlreadyAuditUsers', blank=True)
    technical = models.CharField(max_length=100, verbose_name='职称', default='')
    telephone = models.CharField(max_length=100, verbose_name='联系电话', default='')
    scheduling_type = models.CharField('排班类型', max_length=10, choices=TYPE, default='1')
    stop_patient = models.CharField(max_length=100, verbose_name='停诊人', default='')
    stop_reason = models.CharField(max_length=100, verbose_name='停/换诊原因', default='')
    change_patient = models.CharField(max_length=100, verbose_name='换诊人', default='')
    patient_technical = models.CharField(max_length=100, verbose_name='换诊人职称', default='')
    patient_telephone = models.CharField(max_length=100, verbose_name='换诊人联系电话', default='')
    patient_start_time = models.CharField(max_length=100, verbose_name='换诊开始时间', default='')
    patient_end_time = models.CharField(max_length=100, verbose_name='换诊结束时间', default='')
    stop_start_time = models.CharField(max_length=100, verbose_name='停诊开始时间', default='')
    stop_end_time = models.CharField(max_length=100, verbose_name='停诊结束时间', default='')
    note = models.CharField(max_length=255, verbose_name='备注', default='')
    can_revise = models.CharField(max_length=10, verbose_name='是否可以修改', default='1')
    fj_number = models.CharField('附件编号', max_length=128, blank=True)

    def get_state_str(self):
        if self.state == 'refuse':
            return '拒绝'
        elif self.state == 'end':
            return '结束'
        elif self.state == 'stop':
            return '终止'
        elif self.state == 'draft':
            return '草稿'
        else:
            return '审核中'


class OutpatientSchedulingRecord(BaseModel):
    STATE = (
        ('draft', '拒绝'),
        ('audit', '同意'),
        ('unaudit', '待审核'),
    )
    NODE = (
        ('one', '科室负责人'),
        ('two', '门诊部办公室')
    )
    parent_id = models.ForeignKey('OutpatientScheduling', verbose_name="门诊换班", blank=False, null=False, on_delete=models.CASCADE)
    state = models.CharField('状态', max_length=10, choices=STATE, default='unaudit')
    audit_people = models.ForeignKey('personal_center.User', verbose_name="审批人", blank=False, null=False, on_delete=models.CASCADE)
    audit_note = models.TextField(verbose_name="审批意见", blank=False)
    node = models.CharField('节点', max_length=10, choices=NODE, default='one')

    class Meta:
        managed = True
        verbose_name = '门诊换班'
        verbose_name_plural = verbose_name